package com.common.pay;

import android.app.Activity;
import android.text.TextUtils;

public class PayUtils {

    //支付失败
    public static final int PAY_ERROR = 0x001;
    //支付网络连接出错
    public static final int PAY_NETWORK_ERROR = 0x002;
    //支付结果解析错误
    public static final int RESULT_ERROR = 0x003;
    //正在处理中
    public static final int PAY_DEALING = 0x004;
    //其它支付错误
    public static final int PAY_OTHER_ERROR = 0x006;
    //支付参数异常
    public static final int PAY_PARAMETERS_ERROE = 0x007;
    //取消支付
    public static final int PAY_CANCEL = 0x008;
    //未安装微信或微信版本过低
    public static final int WEIXIN_VERSION_LOW = 0x009;

    private static PayUtils mJPay;
    private Activity mContext;

    private PayUtils(Activity context) {
        mContext = context;
    }

    public static PayUtils getInstance(Activity context) {
        if (mJPay == null) {
            synchronized (PayUtils.class) {
                if (mJPay == null) {
                    mJPay = new PayUtils(context);
                }
            }
        }
        return mJPay;
    }

    /**
     * 发起支付
     *
     * @param payType       支付类型{@link PayType}
     * @param payParamsBean 支付参素
     * @param listener
     */
    public void pay(PayType payType, PayParamsBean payParamsBean, PayListener listener) {
        if (payType == PayType.ALIPAY) {
            toAliPay(payParamsBean.getAlipay_params(), listener);
        } else {
            toWxPay(payParamsBean.getWx_app_params(), listener);
        }
    }

    public void toAliPay(String payParameters, PayListener listener) {
        if (payParameters != null) {
            if (listener != null) {
                Alipay.getInstance(mContext).startAliPay(payParameters, listener);
            }
        } else {
            if (listener != null) {
                listener.onPayError(PAY_PARAMETERS_ERROE, "参数异常");
            }
        }
    }


    public void toWxPay(WxPayInfoBean payParameters, PayListener listener) {
        if (payParameters != null) {
            if (TextUtils.isEmpty(payParameters.getAppId()) || TextUtils.isEmpty(payParameters.getPartnerId())
                    || TextUtils.isEmpty(payParameters.getPrepayId()) || TextUtils.isEmpty(payParameters.getNonceStr())
                    || TextUtils.isEmpty(payParameters.getTimeStamp()) || TextUtils.isEmpty(payParameters.getPaySign())) {
                if (listener != null) {
                    listener.onPayError(PAY_PARAMETERS_ERROE, "参数异常");
                }
                return;
            }
            toWxPay(payParameters.getAppId(),
                    payParameters.getPartnerId(), payParameters.getPrepayId(),
                    payParameters.getNonceStr(), payParameters.getTimeStamp(),
                    payParameters.getPaySign(), listener);

        } else {
            if (listener != null) {
                listener.onPayError(PAY_PARAMETERS_ERROE, "参数异常");
            }
        }
    }

    public void toWxPay(String appId, String partnerId, String prepayId,
                        String nonceStr, String timeStamp, String sign, PayListener listener) {
        if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(partnerId)
                || TextUtils.isEmpty(prepayId) || TextUtils.isEmpty(nonceStr)
                || TextUtils.isEmpty(timeStamp) || TextUtils.isEmpty(sign)) {
            if (listener != null) {
                listener.onPayError(PAY_PARAMETERS_ERROE, "参数异常");
            }
            return;
        }
        WeiXinPay.getInstance(mContext).startWXPay(appId, partnerId, prepayId, nonceStr, timeStamp, sign, listener);
    }

    public interface PayListener {
        //支付成功
        void onPaySuccess();

        //支付失败
        void onPayError(int errorCode, String message);
    }

    public interface AuthListener {
        //授权成功
        void onAuthSuccess(String code);

        //授权失败
        void onAuthError(int errorCode, String message);
    }
}